package com.flowerSystem.controller;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flowerSystem.entity.Coupon;
import com.flowerSystem.entity.Usercoupon;
import com.flowerSystem.service.CouponService;
import com.flowerSystem.util.PageHelper;
import com.flowerSystem.util.ResponseResult;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜花遇上牛战士
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/flower")
@CrossOrigin
public class CouponController {
    @Autowired
    CouponService couponService;

    /**
     *  查询所有优惠券
     * @param pageSize 页容
     * @param pageNumber 页码
     * @return
     */
    @GetMapping("/coupon")
    public ResponseResult selectCoupon(@RequestParam Integer pageSize,@RequestParam Integer pageNumber){
        Page<Coupon> page = couponService.page(new Page<>(pageNumber,pageSize));
        PageHelper pageHelper = new PageHelper<>();
        pageHelper.setData(page.getRecords());
        pageHelper.setPageSize((int) page.getSize());
        pageHelper.setTotal((int) page.getTotal());
        pageHelper.setSize((int) page.getPages());


        return ResponseResult.ok(pageHelper);
    }
    /**
     * 添加优惠券
     */
    @PostMapping("/coupon")
    public ResponseResult postCoupon(@RequestBody Coupon coupon){
        boolean f = couponService.save(coupon);
        if (f){
            return ResponseResult.ok(coupon);
        }
        return ResponseResult.fail();
    }
    /**
     * 修改优惠券
     */
    @PutMapping("/coupon")
    public ResponseResult putCoupon(@RequestBody Coupon coupon){
        boolean f = couponService.updateById(coupon);
        if (f){
            return ResponseResult.ok(coupon);
        }
        return ResponseResult.fail();
    }

    /**
     * 删除优惠券
     */
    @DeleteMapping("/coupon")
    public ResponseResult deleteCoupon(@RequestBody Coupon coupon){
        boolean f = couponService.removeById(coupon);
        if (f){
            return ResponseResult.ok(coupon);
        }
        return ResponseResult.fail();
    }

    /**
     * 领取优惠券
     */
    @GetMapping("/coupon/receive")
    public ResponseResult receive(int id){
        boolean f =couponService.receive(id);
        if (f){
            return ResponseResult.ok(id);
        }
        return ResponseResult.fail();
    }

    //用户查询优惠券
    @GetMapping("/coupon/getCouponByUserId")
    public ResponseResult getCouponByUserId(Integer userId){
        List<Coupon> couponList = couponService.getCouponByuserId(userId);
        return ResponseResult.ok(couponList);
    }
    //用户添加优惠券
    @PostMapping("/user/upUserCoupon")
    public ResponseResult upUserCoupon(@RequestBody Usercoupon usercoupon){
        boolean f = couponService.upUserCoupon(usercoupon.getUserId(),usercoupon.getCouponId());
        if (f){
            return ResponseResult.ok(null);
        }
        return ResponseResult.fail();

    }







}
